<template>
  <div>
    <el-dialog title="考核" :visible.sync="dialogVisible" width="1400px" :close-on-click-modal="false">
      <div class="card-box">
        <div class="data-title">基本信息</div>
        <el-form ref="editForm" :model="form" size="small" :rules="rules">
          <div class="form-wrap">
            <el-form-item label="考核编号" prop="assessCode">
              <el-input disabled v-model="form.assessCode" maxlength="100"></el-input>
            </el-form-item>
            <el-form-item label="考核名称" prop="assessName">
              <el-input disabled v-model="form.assessName" maxlength="32"></el-input>
            </el-form-item>
            <el-form-item label="考核单位" prop="customerName">
              <el-input disabled v-model="form.customerName"></el-input>
            </el-form-item>
            <el-form-item label="考核时间" prop="assessDate">
              <el-input disabled v-model="form.assessDate"></el-input>
            </el-form-item>
            <el-form-item label="考核分数" prop="score">
              <el-input disabled v-model="form.score" :min="0" :max="100"></el-input>
            </el-form-item>
            <el-form-item label="考核结果" prop="assessResult">
              <el-input disabled v-model="form.assessResult"></el-input>
            </el-form-item>
            <el-form-item label="备注" prop="remarks">
              <el-input v-model="form.remarks" maxlength="255"></el-input>
            </el-form-item>
          </div>
        </el-form>
        <div class="data-title" style="padding-top: 30px">考核内容</div>
        <el-table :data="tableData" align="center">
          <el-table-column label="序号" type="index" width="80" align="center"></el-table-column>
          <el-table-column label="考核" prop="item" width="120" align="center"></el-table-column>
          <el-table-column width="120" label="权重%" align="center" prop="weightValue"></el-table-column>
          <el-table-column label="评分说明" width="300" align="center" prop="templateRemarks"></el-table-column>
          <el-table-column width="150" label="评分" align="center" prop="score">
            <template slot-scope="scope">
              <br />
              <span>
                <el-input-number v-model="scope.row.score" :min="0" :max="100" placeholder="请输入"></el-input-number>
              </span>
            </template>
          </el-table-column>
        </el-table>
        <div style="text-align: right">
          <el-button type="primary" size="small" @click="sum">计算考核结果分数</el-button>
        </div>
      </div>
      <div class="card-box" style="text-align: right;padding-top: 40px">
        <el-button type="primary" size="small" v-throttle="[submit]">保存</el-button>
        <el-button type="primary" size="small" @click="close">关闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { putObj, getObj } from '../api/assess-result'
export default {
  name: "edit-result",
  data () {
    var validateNewScore = (rule, value, callback) => {
      if (value === '') {
        callback('请在下面表格输入分数')
      } else {
        callback()
      }
    }
    return {
      dialogVisible: false,
      form: {},
      tableData: [],
      assessTemplate: {},
      rules: {
        score: [{ validator: validateNewScore, trigger: 'blur' }]
      }
    }
  },
  methods: {
    show (item) {
      this.dialogVisible = true
      if (item) {
        this.form = item;

        getObj(item.assessResultId).then(res => {

          //alert(JSON.stringify(res))
          this.tableData = res.data.data
          //this.tableData=res.data.data.
        })
      }
    },
    submit () {
      this.$refs.editForm.validate(valid => {
        if (valid) {
          putObj(Object.assign({ 'assessResultVo': this.form, 'assessResultDetails': this.tableData })).then(res => {
            console.log(res)
            this.dialogVisible = false
          })
        }
      })
    },
    sum () {
      let score = 0
      this.tableData.map(data => {
        if (data.score === '' || data.score == undefined) {
          alert("分数不能为空")
        } else if (parseInt(data.score) > 100) {
          alert("填入的分数不能大于100")
        } else {
          score += parseInt(data.score) * parseInt(data.weightValue) / 100
        }

      })
      this.form.score = score.toFixed(2)
      if (this.form.score < this.form.scoreLevel1) {
        this.form.assessResult = '不合格'
      } else {
        this.form.assessResult = '合格'
      }
    },
    close () {
      this.dialogVisible = false
      this.$emit('close')
    }
  }
}
</script>

<style lang='scss' scoped>
@include data-title();
@include info-list(3);
@include formWrap(4);
</style>
